import 'dart:convert';
import 'package:crypto/crypto.dart';


class Md5Utils{
  static String getSign(Map parameter) {
    var Key = 'android4560';
    var timestamp = new DateTime.now().millisecondsSinceEpoch;
    var versionNumber = 'app-v1';
    parameter['timestamp'] = timestamp.toString();
    parameter['versionNumber'] = versionNumber;
    /// 存储所有key
    List<String> allKeys = [];
    parameter.forEach((key,value){
      allKeys.add(key + value);
    });
    /// key排序
    allKeys.sort((obj1,obj2){
      return obj1.compareTo(obj2);
    });
    // /// 存储所有键值对
    // List<String> pairs = [];
    // /// 添加键值对
    // allKeys.forEach((key){
    //   pairs.add("$key${parameter[key]}");
    // });
    /// 数组转string
    String pairsString = allKeys.join("");
    /// 拼接 ABC 是你的秘钥
    String sign = Key + pairsString + Key;
    /// hash
    String signString = generateMd5(sign).toUpperCase();
    //String signString = md5.convert(utf8.encode(sign)).toString().toUpperCase();  //直接写也可以
    return signString;
  }
  /// md5加密
  static String generateMd5(String data) {
    var content = new Utf8Encoder().convert(data);
    var digest = md5.convert(content);
    // 这里其实就是 digest.toString()
    return digest.toString();
  }
}